package com.lcx.plugin.neteaseCloud.component.toolwindow.login.qr;

import cn.hutool.core.codec.Base64;
import com.intellij.openapi.ui.DialogWrapper;
import com.lcx.plugin.neteaseCloud.component.toolwindow.CustomRectangle;
import com.lcx.plugin.neteaseCloud.component.toolwindow.ImagePanel;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import com.lcx.plugin.neteaseCloud.music.UserInfo;
import com.lcx.plugin.neteaseCloud.music.api.UserApi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public class QrCodeDialogWrapper extends DialogWrapper {

    public JPanel panel;

    public QrCodeDialogWrapper() {
        super(true);
        init();
        setTitle("登录 (2/2)");
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        if (UserApi.getKey()) {
            String qrCode = UserApi.createQRCode();
            if (Objects.isNull(qrCode)) {
                // 生成二维码失败，直接返回panel
                return panel;
            }
        }

        String splitStr = UserApi.qrCodeStr.split(",")[1];
        System.out.println(splitStr);
        JPanel avatarPanel = new ImagePanel(
                new ImageIcon(Base64.decode(splitStr)),
                new CustomRectangle(0, 0, 200, 200));
        panel.add(avatarPanel, BorderLayout.CENTER);
        panel.setPreferredSize(new Dimension(205, 215));

        new Thread(() -> {
            while (true) {
                // 循环检测二维码状态
                int qrStatus = UserApi.checkQRStatus();
                try {
                    // 每隔0.5s检查一次
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (Objects.equals(qrStatus, 801) || Objects.equals(qrStatus, 802)) {
                    // 801为等待扫码，802为待确认
                    continue;
                } else if (Objects.equals(qrStatus, 803)) {
                    // 803为授权成功，根据cookie获取用户信息
                    UserInfo.loginByQrCode();
                    // 刷新面板
                    try {
                        DataCenter.mainPanel.setMyPanel();
                        DataCenter.mainPanel.setMusicListPanel();
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                    break;
                } else {
                    // -1为报错，800为二维码过期，则取消
                    break;
                }
            }
        }).start();
        return panel;
    }

    /**
     * 覆盖默认的ok/cancel按钮
     *
     * @return
     */
    @NotNull
    @Override
    protected Action[] createActions() {
        // 设置默认的焦点按钮
        return new Action[]{null, null};
    }
}
